package com.sikaryofficial.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.sikaryofficial.backend.domain.dto.resp.music.MusicGenreVO;
import com.sikaryofficial.backend.domain.entity.MusicGenreRelation;
import com.sikaryofficial.backend.mapper.MusicGenreRelationMapper;
import com.sikaryofficial.backend.service.IMusicGenreRelationService;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 音乐分类关系Service业务层处理
 *
 * @author qinjinyuan
 * @date 2024-08-07
 */
@Service
public class MusicGenreRelationServiceImpl extends ServiceImpl<MusicGenreRelationMapper, MusicGenreRelation> implements IMusicGenreRelationService {
    @Autowired
    private MusicGenreRelationMapper musicGenreRelationMapper;

    /**
     * 批量保存 音乐分类关系
     *
     * @param musicGenreList
     * @param musicId
     * @return
     */
    @Override
    public boolean batchSaveGenreRelation(List<MusicGenreVO> musicGenreList, Long musicId) {
        // 保存分类关系数据
        this.update(new LambdaUpdateWrapper<MusicGenreRelation>()
                .eq(MusicGenreRelation::getMusicId, musicId)
                .eq(MusicGenreRelation::getDeletedVersion, 0L)
                .set(MusicGenreRelation::getUpdatedBy, SecurityUtils.getUserId())
                .set(MusicGenreRelation::getUpdatedTime, new Date())
                .setSql(" deleted_version = relation_id ")
        );
        List<MusicGenreRelation> musicGenreRelations = Lists.newArrayList();
        musicGenreList.forEach(genre -> {
                    MusicGenreRelation relation = new MusicGenreRelation();
                    musicGenreRelations.add(relation);
                    relation.setMusicId(musicId);
                    relation.setMusicGenreId(genre.getMusicGenreId());
                    relation.setCreatedBy(SecurityUtils.getUserId());
                    relation.setCreatedTime(new Date());
                }
        );
        this.saveBatch(musicGenreRelations);
        return false;
    }
}
